package com.beetlestech.nfe.main.biz.com.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbDiscount;
import com.beetlestech.nfe.main.biz.com.dto.DiscountDTO;
import com.beetlestech.nfe.main.biz.com.dto.MstactivityInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.CoursediscountDTO;
import com.beetlestech.nfe.main.db.tbl.MstactivityDTO;




public class HbDiscountImpl extends AbstractDBLogic implements HbDiscount, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "折扣信息表";
	//查询所有折扣信息
	@Override
	public DiscountDTO getDiscountInfo(){
		
		CoursediscountDTO discountDTO = new CoursediscountDTO();
		List<CoursediscountDTO> listd= queryDAO.queryForObjectList(CoursediscountDTO.EQUAL_SEARCH, discountDTO);
		DiscountDTO dDTO=new DiscountDTO();
		dDTO.setListdiscount(listd);
		return dDTO;
	}
	//新增折扣率
	@Override
	public void saveDiscount(Integer start,Integer end,Double discount){
		
		CoursediscountDTO discountDTO = new CoursediscountDTO();
		discountDTO.setstartcount(start);
		discountDTO.setendcount(end);
		discountDTO.setdiscount(discount);
		
		updateDAO.updateOne(CoursediscountDTO.INSERT, discountDTO,M10190WS, SH_INFO);
	}
	@Override
	public void deleteDiscount(Integer cdId){
		CoursediscountDTO discountDTO = new CoursediscountDTO();
		discountDTO.setcoursediscountid(cdId);
		discountDTO=queryDAO.queryForObject(CoursediscountDTO.EQUAL_SEARCH, discountDTO);
		updateDAO.update(CoursediscountDTO.DELETE, discountDTO);
	}
	//取出所有活动
	@Override
	public  MstactivityInfoDTO[] getMstactivityList(String content,String acDate){
		MstactivityDTO ms=new MstactivityDTO();
		StatementDTO statementDTO = new StatementDTO();
		if(content!=null && !content.equals("")){
			StatementUtil.andWhere(statementDTO, MstactivityDTO.COLUMN_AC_CONTENT, StatementUtil.KEYWORD.LIKE ,content);
		}
		if((acDate!=null &&!acDate.equals(""))){
			StatementUtil.andWhere(statementDTO, MstactivityDTO.COLUMN_AC_DATE, StatementUtil.KEYWORD.EQUAL ,acDate);
		}
		
		ms.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		List<MstactivityDTO> listd= queryDAO.queryForObjectList(MstactivityDTO.SQL_SEARCH, ms);
		List<MstactivityInfoDTO> listm=new ArrayList<MstactivityInfoDTO>();
		for(int i=0;i<listd.size();i++){
			MstactivityInfoDTO mm=new MstactivityInfoDTO();
			mm.setacid(listd.get(i).getacid());
			mm.setaccontent(listd.get(i).getaccontent());
			mm.setacdate(listd.get(i).getacdate());
			listm.add(mm);
		}
		MstactivityInfoDTO[] arr = (MstactivityInfoDTO[])listm.toArray(new MstactivityInfoDTO[listm.size()]);
		
		return arr;
	}
	//删除活动
	@Override
	public void deleteActivity(String actionType,String acSql,Integer ac_id){
		if(actionType.equals("single")){
			MstactivityDTO mDTO =new MstactivityDTO();
			if(ac_id==null){
				return ;
			}
			mDTO.setacid(ac_id);
			updateDAO.updateOne(MstactivityDTO.DELETE, mDTO, M10190WS, SH_INFO);
			
		}else if(actionType.equals("many")){
			String[] dbSqlArray=acSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				if(StringUtil.isEmpty(dbSqlArray[i])){
					break;
				}
				MstactivityDTO mDTO =new MstactivityDTO();
				mDTO.setacid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.updateOne(MstactivityDTO.DELETE, mDTO, M10190WS, SH_INFO);
			}
		}
	}
	//新增活动
	@Override
	 public void savaActivity(String content,String acTime){
		MstactivityDTO ma=new MstactivityDTO();
		ma.setaccontent(content);
		DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");            
        try {
        		Date date = fmt.parse(acTime);
			  java.sql.Date sendSql= new java.sql.Date(date.getTime());
			  ma.setacdate(sendSql);     //开始时间
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         updateDAO.updateOne(MstactivityDTO.INSERT, ma,M10190WS, SH_INFO);
	}
	
	//修改活动
	@Override
	public void updateActivity(Integer acId,String content,String acTime){
		MstactivityDTO ma=new MstactivityDTO();
		ma.setacid(acId);
		ma=queryDAO.queryForObject(MstactivityDTO.EQUAL_SEARCH, ma);
		
		ma.setaccontent(content);
		DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");            
        try {
        	  Date date = fmt.parse(acTime);
			  java.sql.Date sendSql= new java.sql.Date(date.getTime());
			  ma.setacdate(sendSql);     //开始时间
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		updateDAO.updateOne(MstactivityDTO.UPDATE, ma, M10190WS, SH_INFO);
	}
	
	//根据id 查询活动
	@Override
	public MstactivityInfoDTO getActivityInfo(Integer acId){
		MstactivityDTO ma=new MstactivityDTO();
		ma.setacid(acId);
		ma=queryDAO.queryForObject(MstactivityDTO.EQUAL_SEARCH, ma);
		MstactivityInfoDTO mai=new MstactivityInfoDTO();
		mai.setacid(acId);
		mai.setaccontent(ma.getaccontent());
		mai.setacdate(ma.getacdate());
		
		
		return mai;
	}
	
}
